<?php
namespace Admin\Controller;
use Admin\AppConst\WebConst;
use Admin\Service\MenuService;

class MenuController extends BaseController
{
    private $listUrl;
    public function __construct()
    {
        parent::__construct();
        $this->actionTitle  = '菜单';
        $this->assign('actionTitle', $this->actionTitle);

        $this->listUrl  = '/Admin/Menu/index';
        $this->assign('listUrl', $this->listUrl);
    }

    /* @function    列表
     * @date        2018-08-01
     * @author      zhangle
     */
    public function index(){
        $list = MenuService::get()->getList();
        $this->assign('list', $list);
        $this->display();
    }

    /* @function    创建
     * @date        2018-08-02
     * @author      zhangle
     */
    public function createMenu(){
        if(empty($_POST)){
            $list = MenuService::get()->getListSelect();
            $this->assign('list', $list);
            $this->display();
            exit;
        }

        //开始创建
        $pid        = intval($this->getParams['pid']);      //分类id
        $iconName   = $this->getParams['iconName'];         //icon名称
        $name       = $this->getParams['name'];             //菜单名称
        $isShow     = intval($this->getParams['isShow']);   //是否显示

        if(empty($name)){
            $this->error('请填写菜单名称!');
        }

        $sort       = intval($this->getParams['sort']); //排序数值
        $url        = empty($this->getParams['url']) ? '#' : $this->getParams['url'];   //跳转url

        $result     =   MenuService::get()->createMenu($pid, $iconName, $name, $sort, $url, $isShow);

        if($result == WebConst::DEFAULT_TYPE_TRUE){
            $this->success('创建成功!', $this->listUrl);
        }else{
            $this->error('创建失败!');
        }

    }

    /* @function    更新
     * @date        2018-08-02
     * @author      zhangle
     */
    public function editMenu(){

        $id = intval($this->getParams['id']);
        if($id < WebConst::CATEGORY_TYPE_DEFAULT){
            $this->error('id参数错误!');
        }

        if(empty($_POST)){
            //获取详情
            $menu   = MenuService::get()->getMenuById($id);
            $list   = MenuService::get()->getListSelect($menu['pid']);
            $this->assign('list', $list);
            $this->assign('data', $menu);
            //获取当前数据

            $this->display();
            exit;
        }

        //开始更新

        $pid        = intval($this->getParams['pid']);      //分类id
        $iconName   = $this->getParams['iconName'];         //icon名称
        $name       = $this->getParams['name'];             //菜单名称
        $isShow     = intval($this->getParams['isShow']);   //是否显示

        if(empty($name)){
            $this->error('请填写菜单名称!');
        }

        $sort       = intval($this->getParams['sort']); //排序数值
        $url        = empty($this->getParams['url']) ? '#' : $this->getParams['url'];   //跳转url

        $result     =   MenuService::get()->editMenu($id, $pid, $iconName, $name, $sort, $url, $isShow);

        if($result == WebConst::DEFAULT_TYPE_TRUE){
            $this->success('更新成功!', $this->listUrl);
        }else{
            $this->error('更新失败!');
        }

    }

    /* @function    删除
     * @date        2018-08-02
     * @author      zhangle
     */
    public function deleteMenu(){

        $id = intval($this->getParams['id']);
        if($id < WebConst::CATEGORY_TYPE_DEFAULT){
            $this->error('id参数错误!');
        }

        $list   = MenuService::get()->getListByPid($id);

        if(!empty($list)){
            $this->error($this->getParams['name'].' 菜单下有子菜单,请先删除所有的子菜单!');
        }


        $result     =   MenuService::get()->deleteMenu($id);

        if($result == WebConst::DEFAULT_TYPE_TRUE){
            $this->success('删除成功!', $this->listUrl);
        }else{
            $this->error('删除失败!');
        }

    }
}
